﻿using System;

class BinaryDigitsCount
{
    static void Main()
    {
        char binaryDigit = char.Parse(Console.ReadLine());
        uint numberCount = uint.Parse(Console.ReadLine());
        for (int i = 1; i <= numberCount; i++)
        {
            uint number = uint.Parse(Console.ReadLine());
            string binaryNumberString = Convert.ToString(number, 2);
            int result = 0;
            foreach (char character in binaryNumberString)
            {
                if (character == binaryDigit)
                {
                    result++;
                }
            }
            Console.WriteLine(result);
        }
    }
}